package com.simin.videoplayer;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

import com.simin.videoplayer.video.BDVideoView;
import com.simin.videoplayer.video.listener.SimpleOnVideoControlListener;
import com.simin.videoplayer.video.utils.DisplayUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class VideoDetailActivity extends AppCompatActivity {

    private BDVideoView videoView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_detail);
        videoView = findViewById(R.id.vv);
        VideoDetailInfo videobean = (VideoDetailInfo) getIntent().getSerializableExtra("videobean");
        initplay(videobean);
    }

    private void initplay(VideoDetailInfo videoDetailInfo) {
        videoView.setOnVideoControlListener(new SimpleOnVideoControlListener() {

            @Override
            public void onRetry(int errorStatus) {
                // TODO: 2017/6/20 调用业务接口重新获取数据
                // get info and call method "videoView.startPlayVideo(info);"
            }

            @Override
            public void onBack() {
                onBackPressed();
            }

            @Override
            public void onFullScreen() {
                //横屏视频旋转屏幕
                DisplayUtils.toggleScreenOrientation(VideoDetailActivity.this);
            }
        });

        videoView.startPlayVideo(videoDetailInfo);

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        videoView.onStop();
    }

    @Override
    protected void onStart() {
        super.onStart();
        videoView.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoView.onDestroy();
    }

    @Override
    public void onBackPressed() {
        if (!DisplayUtils.isPortrait(this)) {
            if (!videoView.isLock()) {
                DisplayUtils.toggleScreenOrientation(this);
            }
        } else {
            super.onBackPressed();
        }
    }

}
